package processor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import util.addTargetRequest;
import static processor.BasePageInvestmentProcessor.*;
import static processor.BasePageProcessor.processFirmBase;
import static processor.BasePageRegisterProcessor.ParseBRI;
import static processor.EquityPledgeProcessor.processEquityPledge;
import static processor.FenxiangPageProcessor.processFirmFenxian;
//import static processor.LoginProcessor.isRedirectedToLoginPage;
import static processor.ProductProcessor.processBasicProduct;
import static processor.ProductProcessor.processBasicProductList;
import static processor.SearchPageProcessor.processSearchPage;
import static processor.SusongPageProcessor.processFirmSusong;
import static util.Config.getCrawler_CycleRetryTimes;
import static util.Config.getCrawler_SPIDER_SLEEP_TIME;

public class QichachaPageProcessor implements PageProcessor
{
    public static final String SEARCH_URL = "https://www.qichacha.com/search?key=";
    public static final String FIRM_BASE =".html#base";


    //    public static long Min
//    public QichachaPageProcessor(long shareholderGap,long investmentGap,long changeGap,long pledgeGap){
//        /**
//         * @Description 限制投资关系，股东关系抓取层数
//         * @param investmentGap 投资层数
//         * @param shareholderGap 股东层数
//         * @param changeGap 变更历史长度
//         */
//        this.MinShareholderPriority = this.InitialPriority - shareholderGap;
//        this.MinInvestmentPriority = this.InitialPriority - investmentGap;
//        this.MinChangePriority = this.InitialPriority - changeGap;
//        this.MinPledgePriority = this.InitialPriority - pledgeGap;
//    }
//    public QichachaPageProcessor(long shareholderGap){
//        /**
//         * @Description 限制投资关系，股东关系抓取层数
//         * @param investmentGap 投资层数
//         * @param shareholderGap 股东层数
//         */
//        this.MinShareholderPriority = this.InitialPriority - shareholderGap;
//        this.MinInvestmentPriority = Long.MIN_VALUE;
//    }
    public QichachaPageProcessor(){

    }
    private Site site = Site.me().setSleepTime(Integer.valueOf(getCrawler_SPIDER_SLEEP_TIME()))
            .setDomain("www.qichacha.com")
            .addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0")
            .addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .addHeader("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2")
            .addHeader("Accept-Encoding","gzip, deflate")
            .addHeader("Connection","keep-alive")
            .addHeader("Upgrade-Insecure-Requests","1")
            .setCycleRetryTimes(Integer.valueOf(getCrawler_CycleRetryTimes()))
            .setTimeOut(20*1000)
            .setRetryTimes(30)
            .setRetrySleepTime(1000);
//            .addHeader("Cookie","acw_tc=7a0e2b9b15529895675597147e688a3c103379110162c734212e4f0380;_uab_collina=155298937234717461711325;UM_distinctid=16995614010481-05f17e52629cdd8-4c312f7f-1fa400-16995614011101;QCCSESSID=v7gitaoria7seh0e1bljgj8ru4;hasShow=1;zg_did=%7B%22did%22%3A%20%2216995613965883-0e6c17c449241c8-4c312f7f-1fa400-169956139665db%22%7D;zg_de1d1a35bfa24ce29bbf2c7eb17e6c4f=%7B%22sid%22%3A%201552989370729%2C%22updated%22%3A%201552989400851%2C%22info%22%3A%201552989370732%2C%22superProperty%22%3A%20%22%7B%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22%22%2C%22cuid%22%3A%20%22b9fbb5e43803b9a88088cadf35ca07fb%22%7D;CNZZDATA1254842228=1674009091-1552986211-%7C1552986211;Hm_lvt_3456bee468c83cc63fb5147f119f1075=1552989375;Hm_lpvt_3456bee468c83cc63fb5147f119f1075=1552989401;");
    private void processFirm(Page page){
        /**
         * @Description 对链接进行判断：
         * 如果 是base的公司主页链接：
         *      解析base页面，获得企查查唯一标识unique,company_name
         *      将诉讼，经营情况的链接，发送给爬虫引擎。
         * 如果 是诉讼，经营等链接：
         *      解析诉讼经营页面
         * @param page
         */
//        System.out.println("路由界面");
//        if(isRedirectedToLoginPage(page)){
//            return;
//        }
        if(page.getUrl().toString().contains(FIRM_BASE) )//|| page.getUrl().toString().contains())
        {
            processFirmBase(page);
            //获得公司的id与公司名称，作为参数请求诉讼，经营情况。
            //如果此页面是大陆公司页面，则包含company_name
            //在loginProcessor中登陆状态失效的情况下，返回的unique为空，此时不解析页面
            if(page.getResultItems().getAll().containsKey("unique")){
                if (page.getResultItems().getAll().containsKey("company_name")){
                    String unique = page.getResultItems().get("unique").toString();

                    String company_name = page.getResultItems().get("company_name").toString();
                    // 传参数访问诉讼情况
//                    addTargetRequest.add(page,unique,company_name,"susong",page.getRequest().getPriority()-1);
                    // 传参数访问经营情况
//                    addTargetRequest.add(page,unique,company_name,"fengxian",page.getRequest().getPriority()-1);
                    // 传参数访问投资关系,从第一页开始
                    addTargetRequest.add(page,unique,company_name,"base","touzi",1,page.getRequest().getPriority()-1);
                }else {
                    String unique = page.getResultItems().get("unique").toString();

                    //如果此页面是大陆公司页面，则不包含company_name，访问接口
                    addTargetRequest.add(page,unique,page.getRequest().getExtra("search_word").toString(),
                            "base",page.getRequest().getPriority());
                }
            }
        }else if(page.getUrl().toString().endsWith("tab=base")){
            ParseBRI(page);
            //香港公司情况 继续访问如下项
            String unique = page.getRequest().getExtra("unique").toString();
            String company_name = page.getRequest().getExtra("company_name").toString();
//             传参数访问诉讼情况
//            addTargetRequest.add(page,unique,company_name,"susong",page.getRequest().getPriority()-1);
            // 传参数访问经营情况
//            addTargetRequest.add(page,unique,company_name,"fengxian",page.getRequest().getPriority()-1);
            // 传参数访问投资关系,从第一页开始
//            addTargetRequest.add(page,unique,company_name,"base","touzi",1,page.getRequest().getPriority()-1);
        }
        else if(page.getUrl().toString().endsWith("tab=base&box=touzi")){
            processFirmBaseInvestment(page);
        }else if(page.getUrl().toString().endsWith("tab=susong")){
            processFirmSusong(page);
        }else if(page.getUrl().toString().endsWith("tab=fengxian")){
            processFirmFenxian(page);
            processEquityPledge(page);
        }else if(page.getUrl().toString().contains("product_")) {
            processBasicProduct(page);
            processBasicProductList(page);
        }
        else
        {
            System.out.println("定位到其他页面："+page.getUrl().toString());
        }
    }

    @Override
    public void process(Page page) {
        /**
         * @Description 爬虫入口
         */
        //定位到搜索接口方法
        if (page.getUrl().toString().contains(SEARCH_URL)){
            String Url = processSearchPage(page);
            if(Url.length() > 0){
                addTargetRequest.add(page,
                        Url,
                        page.getResultItems().get("search_word").toString(),
                        page.getRequest().getPriority()
                        );
                page.setSkip(true);
            }else {
//                System.out.println("无相关公司或搜索失败");
            };
        }
        // 定位到各个接口
        else
        {
            processFirm(page);
        }
    }
    @Override
    public Site getSite(){
        return site;
    }

}
